package com.ilop.sthome.mvp.present.scene;

import android.content.Context;

import com.example.common.base.OnCallBackToRefresh;
import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.common.command.SendSceneCommand;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.mvp.contract.scene.AddSceneContract;
import com.ilop.sthome.ui.dialog.BaseDialog;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.SceneDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.Collections;
import java.util.List;

public class AddScenePresenter extends BasePresenterImpl<AddSceneContract.IView> implements AddSceneContract.IPresent {
    private Context mContext;
    private String mDeviceName;
    private String mColorCode = "F0";
    private SceneBean mScene;

    public AddScenePresenter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void getDefaultColorCode(String code) {
        mColorCode = "F"+code;
    }

    @Override
    public void onSendScene(String name, int gateway) {
        List<DeviceInfoBean> mGatewayList = DeviceDaoUtil.getInstance().findAllGateway();
        DeviceInfoBean device = mGatewayList.get(gateway);
        mDeviceName = device.getDeviceName();
        boolean hasSceneName = SceneDaoUtil.getInstance().isHasSceneName(mDeviceName, name);
        if (hasSceneName){
            mView.showToastMsg(mContext.getString(R.string.scene_name_is_repeat));
            return;
        }
        int newSid = getSid();
        if (newSid>11){
            mView.showToastMsg(mContext.getString(R.string.get_max_scene_group));
            return;
        }
        mScene = new SceneBean();
        mScene.setSceneOnly(mDeviceName + "-" + newSid);
        mScene.setModleName(name);
        mScene.setChoice(0);
        mScene.setSid(newSid);
        mScene.setColor(mColorCode);
        mScene.setDeviceName(mDeviceName);
        String code = CoderUtils.increaseScene(mScene);
        SendSceneCommand mSendScene = new SendSceneCommand(mContext, device);
        mSendScene.increaseScene(code.toUpperCase());
    }

    @Override
    public void onSaveScene() {
        SceneDaoUtil.getInstance().getSceneDao().insert(mScene);
    }

    @Override
    public int getSid() {
        List<Integer> list = SceneDaoUtil.getInstance().findAllSceneId(mDeviceName);
        if (list.size()>0){
            return Collections.max(list) +1;
        }else {
            return 3;
        }
    }

    @Override
    public void whetherToSave(String sceneName, int gateway) {
        BaseDialog mDialog = new BaseDialog(mContext, new OnCallBackToRefresh() {
            @Override
            public void onConfirm() {
                onSendScene(sceneName, gateway);
            }

            @Override
            public void onCancel() {
                mView.finishActivity();
            }
        });
        mDialog.setMsg(mContext.getString(R.string.save_modle_or_not));
        mDialog.show();
    }
}
